
package ReadWGZ;
import java.io.ObjectInputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.Set;
import java.util.zip.GZIPInputStream;
import java.io.IOException;
import java.io.ObjectInput;

import instrumentation.*;
public class ReadFilewgz{
	private  Map<String, ConditionClass> classes ;
	public  Map<String, ConditionClass>  read(String name) throws  IOException, ClassNotFoundException {
		try {
			classes = new HashMap<String, ConditionClass>();
			ObjectInput oi = new ObjectInputStream(new GZIPInputStream(new URL(
					name).openStream()));
			ConditionClass bClass = (ConditionClass) oi.readObject();
			classes.put(bClass.getCName(), bClass);
			mapBranchCondition = null;
		} catch (Throwable e) {
			System.err.println("ReadFilewgz.read: " + e.getMessage());
			System.exit(1);
		}

		return classes;	
	}
	private  Map<Integer, Condition> mapBranchCondition;
	public Map<Integer, Condition> buildInternalState(Map<String, ConditionClass>  clazzes) {
		mapBranchCondition = new HashMap<Integer, Condition>();
		for(ConditionClass bClass : classes.values()) {			
			for(Condition c : bClass.getCondition()) {
						if(c instanceof ConditionSwitch){
							ConditionSwitch cSwitch=(ConditionSwitch) c;
							Map<Integer,Integer> cases=cSwitch.getCases();
							for(Integer branchID : cases.keySet()) {											
								mapBranchCondition.put(branchID, c);
							}
							mapBranchCondition.put(cSwitch.getDefaultCase(), c);
						}
						else
							mapBranchCondition.put(c.getBranchId(), c);
				}
			}
		return mapBranchCondition;
	}
	public void displayCondition(){
        TreeMap<Integer,Condition> sorted_map = new TreeMap<Integer,Condition>(mapBranchCondition);
        for (Map.Entry<Integer, Condition> entry : sorted_map.entrySet()) {

			Condition c= sorted_map.get(entry.getKey());
			System.out.print("\t"+c.getCName() + " " +c.getMName().toString() + " ");		
			if(c instanceof ConditionIf )				
				System.out.print("[branch:"+((ConditionIf) c).getBranchId() + "] ");
			else{
				  System.out.print("[branch:"+entry.getKey() + "] ");
				}
			System.out.print(c.getCallee1()+ " ");

			System.out.print(c.getV1()==null ?"null":c.getV1().toString()+ " ");
			if(c instanceof ConditionIf){
				System.out.print(c.getV2()==null?"null":c.getV2().toString());
				System.out.print(" PredicateTpe: "+c.getType().toString());
				
				if(c.getExtraInfo()!=null)
					System.out.print(" "+c.getExtraInfo());
				
			}
			else
				System.out.print("\tcases: " + ((ConditionSwitch) c).getCases().get(entry.getKey()));

			System.out.print( c.getIsCallMethod()?" call Method":"" );
			
			System.out.println("");

				
		}
	}	
  public  static void main(String[] args) {
			try {
				ReadFilewgz m = new ReadFilewgz();
//			String[] url={"file:/C:/MAMDA/MAMDA/./instrumented/Stack/Stack.wgz"};
//				String[] url={"file:/C:/MAMDA/MAMDA2014/./instrumented/shoppingcart/products/Product.wgz",
//						"file:/C:/MAMDA/MAMDA2014/./instrumented/shoppingcart/cart/Cart.wgz",
//						"file:/C:/MAMDA/MAMDA2014/./instrumented/shoppingcart/coupons/ClearanceCoupon.wgz",
//						"file:/C:/MAMDA/MAMDA2014/./instrumented/shoppingcart/coupons/AnniversaryCoupon.wgz"};
			//StdXMLBuilder XMLElement
//		String[] url={"file:/C:/MAMDA/MAMDA/./instrumented/nanoxml/XMLElement.wgz"};
//				String[] url={"file:/C:/MAMDA/MAMDA/./instrumented/Fraction/Fraction.wgz"};
//				String[] url={"file:/C:/MAMDA/MAMDA/./instrumented/BitSet/BitSet.wgz"};
//			String[] url={"file:/C:/MAMDA/MAMDA2014/./instrumented/org/joda/time/format/DateTimeFormat.wgz"};
//				String[] url={"file:/C:/MAMDA/MAMDA2014/./instrumented/org/jdom2/Foo.wgz"};
//				String[] url={"file:/C:/MAMDA/MAMDA2014/./instrumented/StringTokenizer/StringTokenizer.wgz"};
//				String[] url={"file:/C:/MAMDA/MAMDA/./instrumented/TreeMap/TreeMap.wgz"};
//				String[] url={"file:/C:/MAMDA/MAMDA2014/./instrumented/Stack/Stack.wgz"};
			String[] url={"file:/C:/MAMDA/MAMDA2014/./instrumented/language/DoubleMetaphone.wgz"};
//					String[] url={"file:/C:/MAMDA/MAMDA2014/./instrumented/Foo/Foo.wgz"};

//				String[] url={"file:/C:/MAMDA/MAMDA2014/./instrumented/nanoxml/CDATAReader.wgz"};
//				String[] url={"file:/C:/MAMDA/MAMDA2014/./instrumented/identity/PathURN.wgz",
//						"file:/C:/MAMDA/MAMDA2014/./instrumented/identity/URN.wgz",
//						"file:/C:/MAMDA/MAMDA2014/./instrumented/identity/URI.wgz",
//						"file:/C:/MAMDA/MAMDA2014/./instrumented/identity/MIMEType.wgz",
//						"file:/C:/MAMDA/MAMDA2014/./instrumented/identity/ResourceURL.wgz",
//						"file:/C:/MAMDA/MAMDA2014/./instrumented/identity/URIClassifier.wgz",						
//						"file:/C:/MAMDA/MAMDA2014/./instrumented/identity/URIUtil.wgz"};

				for(int i=0;i<url.length;i++){
					 System.out.println( url[i]);
					 m.read(url[i]);
					 m.buildInternalState(null);
					 m.displayCondition();
				}	 
			} catch (Throwable e) {
				System.err.println("IO error: " + e.getMessage());
				System.exit(1);
			}
			System.exit(0);
  }

}